using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL {
/// <summary>
/// The UserProfileTableDB class is responsible for interacting with the database to retrieve and store information
/// about UserProfileTable objects.
/// </summary>
public class UserProfileTableDB

{
#region "Public Methods"

/// <summary>
/// Gets an instance of UserProfileTable from the underlying datasource.
/// </summary>
/// <param name="userProfileKey">The unique UserProfileKey of the UserProfileTable in the database.</param>
/// <returns>An UserProfileTable when the UserProfileKey was found in the database, or null otherwise.</returns>
public static UserProfileTable GetItem(int userProfileKey)
{
UserProfileTable myUserProfileTable = null;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocUserProfileTableSelectSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@userProfileKey", userProfileKey);

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.Read()) {
myUserProfileTable = FillDataRecord(myReader);
}
myReader.Close();
}
myConnection.Close();
}
return myUserProfileTable;
}
}

/// <summary>
/// Returns a list with UserProfileTable objects.
/// </summary>
/// <returns>A generics List with the UserProfileTable objects.</returns>
public static UserProfileTableList GetList()
{
UserProfileTableList tempList = new UserProfileTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocUserProfileTableSelectList", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
}
return tempList;
}

/// <summary>
/// Returns a list with UserProfileTable objects.
/// </summary>
/// <returns>A generics List with the UserProfileTable objects.</returns>
public static UserProfileTableList GetListByBloodGroupCode(short BloodGroupCode)
{
UserProfileTableList tempList = new UserProfileTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocUserProfileTableSelectListByBloodGroupCode", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@bloodGroupCode", BloodGroupCode);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
return tempList;
}
}

/// <summary>
/// Returns a list with UserProfileTable objects.
/// </summary>
/// <returns>A generics List with the UserProfileTable objects.</returns>
public static UserProfileTableList GetListByMaritalStatusCode(short MaritalStatusCode)
{
UserProfileTableList tempList = new UserProfileTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocUserProfileTableSelectListByMaritalStatusCode", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@maritalStatusCode", MaritalStatusCode);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
return tempList;
}
}

/// <summary>
/// Returns a list with UserProfileTable objects.
/// </summary>
/// <returns>A generics List with the UserProfileTable objects.</returns>
public static UserProfileTableList GetListByNationalityCode(short NationalityCode)
{
UserProfileTableList tempList = new UserProfileTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocUserProfileTableSelectListByNationalityCode", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@nationalityCode", NationalityCode);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
return tempList;
}
}

/// <summary>
/// Returns a list with UserProfileTable objects.
/// </summary>
/// <returns>A generics List with the UserProfileTable objects.</returns>
public static UserProfileTableList GetListByRaceCode(short RaceCode)
{
UserProfileTableList tempList = new UserProfileTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocUserProfileTableSelectListByRaceCode", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@raceCode", RaceCode);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
return tempList;
}
}

/// <summary>
/// Returns a list with UserProfileTable objects.
/// </summary>
/// <returns>A generics List with the UserProfileTable objects.</returns>
public static UserProfileTableList GetListByReligionCode(short ReligionCode)
{
UserProfileTableList tempList = new UserProfileTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocUserProfileTableSelectListByReligionCode", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@religionCode", ReligionCode);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
return tempList;
}
}

/// <summary>
/// Returns a list with UserProfileTable objects.
/// </summary>
/// <returns>A generics List with the UserProfileTable objects.</returns>
public static UserProfileTableList GetListByUserAccountKey(int UserAccountKey)
{
UserProfileTableList tempList = new UserProfileTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocUserProfileTableSelectListByUserAccountKey", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@userAccountKey", UserAccountKey);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
return tempList;
}
}

/// <summary>
/// Saves a UserProfileTable in the database.
/// </summary>
/// <param name="myUserProfileTable">The UserProfileTable instance to save.</param>
/// <returns>The new UserProfileKey if the UserProfileTable is new in the database or the existing UserProfileKey when an item was updated.</returns>
public static int Save(UserProfileTable myUserProfileTable)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocUserProfileTableInsertUpdateSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

if (myUserProfileTable.UserProfileKey == -1)
{myCommand.Parameters.AddWithValue("@userProfileKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@userProfileKey", myUserProfileTable.UserProfileKey);
}
if (myUserProfileTable.UserAccountKey == null){
myCommand.Parameters.AddWithValue("@userAccountKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@userAccountKey", myUserProfileTable.UserAccountKey);
}
if (string.IsNullOrEmpty(myUserProfileTable.FirstName))
{
myCommand.Parameters.AddWithValue("@firstName", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@firstName", myUserProfileTable.FirstName);
}
if (string.IsNullOrEmpty(myUserProfileTable.LastName))
{
myCommand.Parameters.AddWithValue("@lastName", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@lastName", myUserProfileTable.LastName);
}
if (string.IsNullOrEmpty(myUserProfileTable.OtherName))
{
myCommand.Parameters.AddWithValue("@otherName", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@otherName", myUserProfileTable.OtherName);
}
if (myUserProfileTable.DateOfBirth == null){
myCommand.Parameters.AddWithValue("@dateOfBirth", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@dateOfBirth", myUserProfileTable.DateOfBirth);
}
if (myUserProfileTable.BloodGroupCode == null){
myCommand.Parameters.AddWithValue("@bloodGroupCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@bloodGroupCode", myUserProfileTable.BloodGroupCode);
}
if (myUserProfileTable.MaritalStatusCode == null){
myCommand.Parameters.AddWithValue("@maritalStatusCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@maritalStatusCode", myUserProfileTable.MaritalStatusCode);
}
if (myUserProfileTable.RaceCode == null){
myCommand.Parameters.AddWithValue("@raceCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@raceCode", myUserProfileTable.RaceCode);
}
if (myUserProfileTable.ReligionCode == null){
myCommand.Parameters.AddWithValue("@religionCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@religionCode", myUserProfileTable.ReligionCode);
}
if (myUserProfileTable.NationalityCode == null){
myCommand.Parameters.AddWithValue("@nationalityCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@nationalityCode", myUserProfileTable.NationalityCode);
}
if (string.IsNullOrEmpty(myUserProfileTable.NRICNumber))
{
myCommand.Parameters.AddWithValue("@nRICNumber", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@nRICNumber", myUserProfileTable.NRICNumber);
}
if (myUserProfileTable.DrivingLicenceType == null){
myCommand.Parameters.AddWithValue("@drivingLicenceType", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@drivingLicenceType", myUserProfileTable.DrivingLicenceType);
}
if (string.IsNullOrEmpty(myUserProfileTable.DrivingLicenseNumber))
{
myCommand.Parameters.AddWithValue("@drivingLicenseNumber", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@drivingLicenseNumber", myUserProfileTable.DrivingLicenseNumber);
}
if (myUserProfileTable.TravelDocumentType == null){
myCommand.Parameters.AddWithValue("@travelDocumentType", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@travelDocumentType", myUserProfileTable.TravelDocumentType);
}
if (string.IsNullOrEmpty(myUserProfileTable.TravelDocumentNumber))
{
myCommand.Parameters.AddWithValue("@travelDocumentNumber", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@travelDocumentNumber", myUserProfileTable.TravelDocumentNumber);
}
if (string.IsNullOrEmpty(myUserProfileTable.WebSiteUrl))
{
myCommand.Parameters.AddWithValue("@webSiteUrl", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@webSiteUrl", myUserProfileTable.WebSiteUrl);
}
if (myUserProfileTable.Status == null){
myCommand.Parameters.AddWithValue("@status", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@status", myUserProfileTable.Status);
}
if (myUserProfileTable.CreatedDateTime == null){
myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@createdDateTime", myUserProfileTable.CreatedDateTime);
}
if (myUserProfileTable.ModifiedDateTime == null){
myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@modifiedDateTime", myUserProfileTable.ModifiedDateTime);
}

DbParameter returnValue;
returnValue = myCommand.CreateParameter();
returnValue.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(returnValue);

myConnection.Open();
myCommand.ExecuteNonQuery();
result = Convert.ToInt32(returnValue.Value);
myConnection.Close();
}
}
return result;
}

/// <summary>
/// Deletes a UserProfileTable from the database.
/// </summary>
/// <param name="userProfileKey">The UserProfileKey of the UserProfileTable to delete.</param>
/// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
public static bool Delete(int userProfileKey)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocUserProfileTableDeleteSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myCommand.Parameters.AddWithValue("@userProfileKey", userProfileKey);
myConnection.Open();
result = myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
return result > 0;
}

#endregion

/// <summary>
/// Initializes a new instance of the UserProfileTable class and fills it with the data fom the IDataRecord.
/// </summary>
private static UserProfileTable FillDataRecord(IDataRecord myDataRecord )
{
UserProfileTable myUserProfileTable = new UserProfileTable();
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("UserProfileKey")))
{
myUserProfileTable.UserProfileKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("UserProfileKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("UserAccountKey")))
{
myUserProfileTable.UserAccountKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("UserAccountKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("FirstName")))
{
myUserProfileTable.FirstName = myDataRecord.GetString(myDataRecord.GetOrdinal("FirstName"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("LastName")))
{
myUserProfileTable.LastName = myDataRecord.GetString(myDataRecord.GetOrdinal("LastName"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("OtherName")))
{
myUserProfileTable.OtherName = myDataRecord.GetString(myDataRecord.GetOrdinal("OtherName"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("DateOfBirth")))
{
myUserProfileTable.DateOfBirth = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("DateOfBirth"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("BloodGroupCode")))
{
myUserProfileTable.BloodGroupCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("BloodGroupCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("MaritalStatusCode")))
{
myUserProfileTable.MaritalStatusCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("MaritalStatusCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("RaceCode")))
{
myUserProfileTable.RaceCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("RaceCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ReligionCode")))
{
myUserProfileTable.ReligionCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("ReligionCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("NationalityCode")))
{
myUserProfileTable.NationalityCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("NationalityCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("NRICNumber")))
{
myUserProfileTable.NRICNumber = myDataRecord.GetString(myDataRecord.GetOrdinal("NRICNumber"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("DrivingLicenceType")))
{
myUserProfileTable.DrivingLicenceType = myDataRecord.GetInt16(myDataRecord.GetOrdinal("DrivingLicenceType"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("DrivingLicenseNumber")))
{
myUserProfileTable.DrivingLicenseNumber = myDataRecord.GetString(myDataRecord.GetOrdinal("DrivingLicenseNumber"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("TravelDocumentType")))
{
myUserProfileTable.TravelDocumentType = myDataRecord.GetInt16(myDataRecord.GetOrdinal("TravelDocumentType"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("TravelDocumentNumber")))
{
myUserProfileTable.TravelDocumentNumber = myDataRecord.GetString(myDataRecord.GetOrdinal("TravelDocumentNumber"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("WebSiteUrl")))
{
myUserProfileTable.WebSiteUrl = myDataRecord.GetString(myDataRecord.GetOrdinal("WebSiteUrl"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
{
myUserProfileTable.Status = myDataRecord.GetInt16(myDataRecord.GetOrdinal("Status"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
{
myUserProfileTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
{
myUserProfileTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
}
return myUserProfileTable;
}
}

 } 
